package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.UserAccountFlowBean;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.mapper.UserAccountFlowMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.NumberUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 结算收款 节点回调
 */
@Service(PjInfoService.PJPAYMENT)
public class PjPaymentCallback implements IPjInfoNodeCallback{

    private static Logger logger = LoggerFactory.getLogger(PjPaymentCallback.class);

    /**
     * 缴费方式 用于转预存插入流水记录
     */
    private static final String PAY_WAY = "1";

    /**
     * 缴费渠道 用于转预存插入流水记录
     */
    private static final String PAY_CHANNEL = "1";

    /**
     * 余额退款方式 转预存
     */
    private static final String REFUND_WAY = "5";

    @Override
    public MessageBean callback(SessionFactory factory, PjInfoBean pjInfoBean, UserInfoBean userInfo) {
        String refundWay = pjInfoBean.getRefundWay();
        // 假如工程结算退款方式为 转预存
        if (Objects.equals(REFUND_WAY, refundWay)){
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            UserAccountFlowMapper userAccountFlowMapper = factory.getMapper(UserAccountFlowMapper.class);

            PjMeterQuery pjMeterQuery = new PjMeterQuery();
            pjMeterQuery.setBalanceRefundFlag(1);
            pjMeterQuery.setConsultId(pjInfoBean.getId());
            // 获取需要转预存的水表账户信息
            List<PjMeter> pjMeterList = pjMeterMapper.getList(pjMeterQuery);
            if(pjMeterList.size() > 0){
                // 将水表表集合里的 userAccountTempId 转成 集合
                List<String> accountInfoTempIdList = pjMeterList.stream().map(t -> t.getUserAccountTempId()).collect(Collectors.toList());
                // 将水表表集合里的 userInfoTempId 转成 集合
                // List<String> userIdList = pjMeterList.stream().map(t -> t.getUserInfoTempId()).collect(Collectors.toList());
                // 通过 userAccountTempId 获取到 临时表的账户信息
                List<AccountInfoBean> accountInfoTempList = accountInfoMapper.getListByIdList(accountInfoTempIdList, AccountInfoBean.ACCOUNTTEMP);
                // 转成账户编号集合
                List<String> accountNoList = accountInfoTempList.stream().map(t -> t.getAccountNo()).collect(Collectors.toList());
                // 通过 账户编号获取到账户信息
                List<AccountInfoBean> accountInfoList = accountInfoMapper.getAccountsByAccountNos(accountNoList);
                // 将临时表账户信息集合转成 map
                Map<String, AccountInfoBean> accountInfoTempMap = accountInfoTempList.stream().collect(Collectors.toMap(AccountInfoBean::getId, Function.identity()));
                // 将账户信息集合转成 map
                Map<String, AccountInfoBean> accountInfoMap = accountInfoList.stream().collect(Collectors.toMap(AccountInfoBean::getAccountNo, Function.identity()));

                for (PjMeter pjMeter : pjMeterList){
                    // 临时表账户信息
                    AccountInfoBean accountInfoTemp = accountInfoTempMap.get(pjMeter.getUserAccountTempId());
                    // 正式表账户信息
                    AccountInfoBean accountInfo = accountInfoMap.get(accountInfoTemp.getAccountNo());
                    // 获取用户编号信息
                    String userNo = userInfoMapper.selectUserNoById(pjMeter.getUserInfoTempId(), UserBean.USERTEMP);
                    double transactionMoney = accountInfoTemp.getAccountBalance() == null ? 0 : accountInfoTemp.getAccountBalance();// 账户余额
                    double priorPeriodBalance = accountInfo.getAccountBalance() == null ? 0 : accountInfo.getAccountBalance(); // 账户表当前余额
                    double currentPeriodBalance = NumberUtils.add(transactionMoney, priorPeriodBalance);
                    if (accountInfoTemp.getAccountBalance() > 0){
                        // 修改账户表的当前余额
                        accountInfoMapper.updateAccountBalance(accountInfo.getAccountNo(), currentPeriodBalance, AccountInfoBean.ACCOUNT);
                        UserAccountFlowBean deductFlow = getAccountFlow(userInfo, accountInfo.getAccountNo(), userNo, transactionMoney, priorPeriodBalance, currentPeriodBalance, "1");
                        // 插入一条预存流水
                        userAccountFlowMapper.save(deductFlow);
                    }
                }
            }
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
    }

    private static UserAccountFlowBean getAccountFlow(UserInfoBean userInfo, String accountNo, String userNo, double transactionMoney, double priorPeriodMoney, double currendPeriodMoney, String payType) {
        UserAccountFlowBean accountFlowBean = new UserAccountFlowBean();
        accountFlowBean.setId(Tools.getObjectId());
        accountFlowBean.setAccountNo(accountNo);
        accountFlowBean.setUserNo(userNo);
        accountFlowBean.setTransactionMoney(transactionMoney);
        accountFlowBean.setPriorPeriodMoney(priorPeriodMoney);
        accountFlowBean.setCurrendPeriodMoney(currendPeriodMoney);
        accountFlowBean.setPayChannel(PAY_CHANNEL);
        accountFlowBean.setPayWay(PAY_WAY);
        accountFlowBean.setPayType(payType);
        accountFlowBean.setChargingStaffAccount(userInfo.getUserInfo().getAcount());
        accountFlowBean.setChargingStaffName(userInfo.getUserInfo().getName());
        accountFlowBean.setComments("工程结算收款预存转余额" + transactionMoney);
        accountFlowBean.setSerialNo(Tools.getObjectId());
        accountFlowBean.setIpAddr(userInfo.getIpAddr());
        accountFlowBean.setCreateInfo(userInfo);
        return accountFlowBean;
    }
}
